package com.djl.seckilldemo.exception;

import com.djl.seckilldemo.vo.RespBean;
import com.djl.seckilldemo.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @program: seckill-demo
 * @description: 异常处理类
 * @author: Mr.dai
 * @create: 2021-10-15 11:39
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public RespBean ExceptionHandler(Exception e){
        if (e instanceof  GlobalException){
            GlobalException ex = (GlobalException) e;
            System.out.println(ex.getRespBeanEnum());
            return RespBean.error(ex.getRespBeanEnum());
        }else if (e instanceof BindException){
            BindException ex=(BindException) e;
            RespBean respBean= RespBean.error(RespBeanEnum.BIND_ERROR);
            respBean.setMessage("参数校验异常"+ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }

        return RespBean.error(RespBeanEnum.ERROR);
    }

}
